<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>夹具报警</title>
<%@include file="/jsp/include/common.jsp"%>
</head>
<body class="easyui-layout">
	<%@include file="/jsp/include/commonLayout.jsp"%>
	<div data-options="region:'center'">
		<div>
			<!-- 数据表格  -->
			<table id="dataGrid"></table>
			<!-- 添加或编辑用户的弹出窗口 begin -->
			<div id="saveOrEditDlg" class="easyui-dialog"
				style="width: 400px; height: 280px; padding: 10px 10px"
				closed="true" buttons="#dlg-buttons" data-options="modal:true">
				<form id="saveOrEditForm" method="post">
					<table>
						<tr>
							<td>夹具编号:</td>
							<td><input id="jiaju_no" name="jiaju_no" class="easyui-textbox"  data-options="required:true"></td>
						</tr>
						<tr>
							<td>夹具名称:</td>
							<td><input id="jiaju_name" name="jiaju_name" class="easyui-textbox"></td>
						</tr>
						<tr>
							<td>夹具单元名称:</td>
							<td><input id="jiaju_unint" name="jiaju_unint" class="easyui-textbox"></td>
						</tr>
						<tr>
							<td>开始使用时间:</td>
							<td><input name="start_time" type="text" class="easyui-datebox"   editable="false" required ="true"></td>
						</tr>
						<tr>
							<td>使用寿命(月):</td>
							<td><input name="life" id="life" ></td>
						</tr>
						<tr>
							<td>提前报警时间(天):</td>
							<td><input name="waring_earlytime" id="waring_earlytime_no" ></td>
						</tr>
					</table>
					<input name="oid" type="hidden" class="">
				</form>
			</div>
			<div id="dlg-buttons">
				<a class="easyui-linkbutton" iconCls="icon-ok"
					onclick="saveOrEditWaring()">保存</a> <a class="easyui-linkbutton"
					iconCls="icon-cancel"
					onclick="javascript:$('#saveOrEditDlg').dialog('close')">取消</a>
			</div>
			<!-- 添加或编辑用户的弹出窗口 end -->
		</div>
		
			<!-- 更换夹具单元 begin -->
			<div id="changeUnitDlg" class="easyui-dialog"
				style="width: 400px; height: 280px; padding: 10px 10px"
				closed="true" buttons="#changeUnitDlg-buttons" data-options="modal:true">
				<form id="changeUnitForm" method="post">
				<table>
						<tr>
							<td>更换时间: </td>
							<td><input name="oname" type="text"   editable="false" class="easyui-datebox"  required ="true"></td>
						</tr>
						<tr>
							<td>夹具开始使用时间:</td>
							<td><input name="start_time" id="time"  disabled="disabled" class="easyui-textbox" ></td>
						</tr>
						<tr>
							<td>上次更换时间:</td>
							<td><input name="change_time"  disabled="disabled"  id="change_time" class="easyui-textbox"></td>
						</tr>
				</table>
					<input name="change_log" type="hidden" id="change_log">
					<input name="oid" type="hidden" >
					<input name="jiaju_no" type="hidden" >
					<input name="jiaju_name" type="hidden" >
					<input name="jiaju_unint" type="hidden" >
					<input name="life" type="hidden" >
					<input name="waring_earlytime" type="hidden" >
						 
						 
				</form>
				
			</div>
			<div id="changeUnitDlg-buttons">
				<a class="easyui-linkbutton" iconCls="icon-ok"
					onclick="saveOrEditchangeUnit()">保存</a> 
				<a class="easyui-linkbutton"
					iconCls="icon-cancel"
					onclick="javascript:$('#changeUnitDlg').dialog('close')">取消</a>
			</div>
			<!-- 更换夹具单元 end -->
			<div id="changeLogDlg" class="easyui-dialog"
				style="width: 400px; height: 280px; padding: 10px 10px"
				closed="true" buttons="#changeLogDlg-buttons" data-options="modal:true">
						<span id="changeLog"></span>
					<input name="change_log" type="hidden" id="change_log">
					<input name="oid" type="hidden" >
			</div>
			<div id="changeLogDlg-buttons">
				<a class="easyui-linkbutton"
					iconCls="icon-ok"
					onclick="javascript:$('#changeLogDlg').dialog('close')">确定</a>
			</div>
	</div>
</body>
<script type="text/javascript">


	$(document).ready(function() {
		// 初始化表格
		var dataGrid = $('#dataGrid').datagrid({
			rownumbers : true,	//如果为true，则显示一个行号列
			fitColumns : true,	//真正的自动展开/收缩列的大小，以适应网格的宽度，防止水平滚动
			collapsible : true,	//定义是否显示可折叠按钮
			title : '夹具报警信息管理',	//在面板头部显示的标题文本
			pagination : true,	//如果为true，则在DataGrid控件底部显示分页工具栏
			pagePosition : 'bottom',	//定义分页工具栏的位置
			method : 'post', //该方法类型请求远程数据
			url : getRootPath() + '/WaringController/getWaringList.do',	//一个URL从远程站点请求数据 
			singleSelect : false,//只能选一行
			striped : true, //数据条纹显示
			frozenColumns : [ [ {//不可被删除的列
				field : 'ck',	//列字段名称
				checkbox : true	//如果为true，则显示复选框
			} ] ],
			columns : [ [ {
				field : 'jiaju_no',
				title : '夹具号',
				width : 150,
				sortable : true	//如果为true，则允许列使用排序
			}, {
				field : 'jiaju_name',
				title : '夹具名称',
				width : 150
			}, {
				field : 'jiaju_unint',
				title : '单元名称',
				width : 150
			}, {
				field : 'start_time',
				title : '开始使用时间',
				width : 100
			}, {
				field : 'change_log',
				title : 'change_log',
				width : 200,
				hidden : true
			} , {
				field : 'change_time',
				title : '最新更换时间',
				width : 100
			} ,
			{
				field : 'life',
				title : '夹具寿命',
				width : 70,
				formatter: function(value,row,index){ return value+"个月";}
			},  {
				field : 'oid',
				title : 'oid',
				hidden : true
			} ,{
				field : 'waring_earlytime',
				title : '报警时间',
				width : 50,
				formatter: function(value,row,index){ return "提前"+value+"天";}
			},{
				field : 'odesc',
				title : '报警状态',
				width : 50,
				hidden : true,
				formatter: function(value,row,index){ return "提前"+value+"天";}
			}
			] ],
			sortName : 'oid',	//定义哪些列可以进行排序
			toolbar : [{	//顶部工具栏的DataGrid面板
				text : '添加',
				iconCls : 'icon-add',
				handler : function(){
					
					$('#saveOrEditDlg').dialog('open').dialog('setTitle', '新建报警');
					$('#saveOrEditForm').form('clear');
					$('#waring_earlytime_no').numberbox({
						min:0,    
					    max:100,
					   // suffix:"天",
					    value:30,
					    required: true, 
					    precision:0    
					});
					$('#life').numberbox({    
						min:0,    
					    max:500,
					    //suffix:"月",
					    required: true, 
					    value:12,
					    precision:0     
					});  
					 
				}
			},{
				text : '编辑',
				iconCls : 'icon-edit',
				handler : function(){
					$("#oid").textbox('disableValidation');
					var rows = $('#dataGrid').datagrid('getSelections');
					if (rows.length == 0) {
						$.messager.alert('提示', '请选择修改项', 'info');
						return;
					} else if (rows.length > 1) {
						$.messager.alert('提示', '只能选择一项', 'info');
						return;
					}
					if (rows) {
						$('#saveOrEditDlg').dialog('open').dialog('setTitle', '编辑夹具报警信息');
						$.ajax({
							type : 'post',
							async : false,
							url : getRootPath() + '/WaringController/findWaringById.do',
							data : rows[0],
							success : function(result) {
								$('#saveOrEditForm').form('load', result);
							},
							dataType : 'json'
						});
					}
				}
			},{
				text : '删除',
				iconCls : 'icon-remove',
				handler : function(){
					var rows = $('#dataGrid').datagrid('getSelections');
					if (rows.length == 0) {
						$.messager.alert('提示', '请选择删除项', 'info');
						return;
					}

					var oids = [];
					for (var i = 0; i < rows.length; i++) {
						oids.push(rows[i].oid);
					}

					oids = oids.join('-');

					$.messager.confirm('确认', '确定要删除选中的信息吗?', function(r) {
						if (r) {
							$.post(getRootPath() + '/WaringController/removeWarings.do', {
								'oids' : oids
							}, function(result) {
								//result = $.parseJSON(result);
								if (result.successful) {
									$('#dataGrid').datagrid('reload'); // reload the Waring data
								} 
								$.messager.show(result);
							}, 'json');
						}
					});
				}
			},'-',{
				text : '更换夹具单元',
				handler : function() {
					var rows = $('#dataGrid').datagrid('getSelections');
					if (rows.length == 0) {
						$.messager.alert('提示','请选择操作项','info');
						return;
					} else if (rows.length > 1) {
						$.messager.alert('提示','只能选择一项','info');
						return;
					}
					id = rows[0].oid;
					if (rows) {
						$('#changeUnitDlg').dialog('open').dialog('setTitle', '更换夹具单元');
						$.ajax({
							type : 'post',
							async : false,
							url : getRootPath() + '/WaringController/findWaringById.do',
							data : rows[0],
							success : function(result) {
								$('#changeUnitForm').form('load', result);
								
							},
							dataType : 'json'
						});
					}
				}
			},'-',{
				text : '查看更换记录',
				handler : function(){
					var rows = $('#dataGrid').datagrid('getSelections');
					if (rows.length == 0) {
						$.messager.alert('提示','请选择操作项','info');
						return;
					} else if (rows.length > 1) {
						$.messager.alert('提示','只能选择一项','info');
						return;
					}
					id = rows[0].oid;
					var log=rows[0].change_log
					log="<p>"+log.replace(/,/g,'</p><p>')+"</p>";
					$('#changeLog').html(log);
					$('#changeLogDlg').dialog('open').dialog('setTitle', '更换记录');
					$('#changeLogDlg').window('open');
				}
			}],
		remoteFilter : true,	//设置为true时，启用远程过滤
		filterDelay : 1000,		//延迟过滤 'text' 过滤器组件中最后一次键盘输入事件
		filterStringify : function(data) {	//字符串化过滤器规则的函数
			return JSON.stringify(data);
		} 
		});

		dataGrid.datagrid('enableFilter');
	});

	// 保存夹具报警
	function saveOrEditWaring() {
		$('#saveOrEditForm').form('submit', {
			url : getRootPath() + '/WaringController/saveOrEditWaring.do',
			onSubmit : function() {
				return $(this).form('validate');
			},
			success : function(result) {
				result = $.parseJSON(result);
				if (result.successful) {
					$('#dataGrid').datagrid('reload'); // reload the Waring data
				} 
				$('#saveOrEditDlg').dialog('close'); // close the dialog
				$.messager.show(result);
			}
		});
	}
	
	function saveOrEditchangeUnit() {
		var time = document.getElementById("time").value;
	//	alert(time);
		$('#changeUnitForm').form('submit', {
			url : getRootPath() + '/WaringController/saveChangeTime.do',
			onSubmit : function(param) {
				//alert(JSON.stringify(param));
				param.start_time = time;
				return $(this).form('validate');
			},
			success : function(result) {
				result = $.parseJSON(result);
				if (result.successful) {
					$('#dataGrid').datagrid('reload'); // reload the Waring data
				} 
				$('#changeUnitDlg').dialog('close'); // close the dialog
			}
		});
	}
	
</script>
</html>
